export const Format = (time, fmt) => {
  let date = new Date(time)
  // date.setTime(time * 1000)
  const o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds()
  }
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

export const isEmpty = (s) => {
  return s === undefined ||
   s === null ||
   s === '' ||
   String(s).toUpperCase() === 'NULL' ||
   (Array.prototype.isPrototypeOf(s) && s.length === 0) ||
   (Object.prototype.isPrototypeOf(s) && Object.keys(s).length) === 0
}

// export const isLogin = () => { // 判断是否登录
//   let sessionToken = store.state.userInfo.sessionToken
//   let regMobile = store.state.userInfo.regMobile
//   if (isEmpty(sessionToken) || isEmpty(regMobile)) {
//     return false
//   } else {
//     return true
//   }
// }

export const stringTrim = (s) => { // 去除收尾空格
  if (s) {
    return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
  }
}

export const maskPhone = (phone) => { // 手机号中间四位掩码
  if (phone) {
    phone = phone.replace(/\s/g, '')
    return phone.substr(0, 3) + ' **** ' + phone.substr(phone.length - 4, 4)
  }
}

export const formatAccount = (accNo) => { // 格式化银行账号(每隔4位加一个空格)
  let formatAcc = ''
  if (!accNo || stringTrim(accNo) === '') {
    return formatAcc
  }
  accNo = stringTrim(accNo.replace(/\s/g, ''))
  let accLength = accNo.length
  let accArray = []
  // 获取需要的空格数
  let spaceCount = (accLength % 4 === 0) ? parseInt(accLength / 4) - 1 : parseInt(accLength / 4)
  for (let i = 0; i < spaceCount; i++) {
    // 每4位数添加进数组
    let fourNumbers = accNo.substr(i * 4, 4)
    accArray.push(fourNumbers)
  }
  // 添加最后的几位数（1~4位）
  accArray.push(accNo.substring(spaceCount * 4, accLength))
  // 组装成格式化账号
  formatAcc = accArray.join(' ')
  return formatAcc
}

export const getScrollTop = () => { // 滚动条在Y轴上的滚动距离
  let scrollTop = 0
  let bodyScrollTop = 0
  let documentScrollTop = 0
  if (document.body) {
    bodyScrollTop = document.body.scrollTop
  }
  if (document.documentElement) {
    documentScrollTop = document.documentElement.scrollTop
  }
  scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop
  return scrollTop
}

export const getScrollHeight = () => { // 获取文档的总高度
  let scrollHeight = 0
  let bodyScrollHeight = 0
  let documentScrollHeight = 0
  if (document.body) {
    bodyScrollHeight = document.body.scrollHeight
  }
  if (document.documentElement) {
    documentScrollHeight = document.documentElement.scrollHeight
  }
  scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
  return scrollHeight
}

export const getWindowHeight = () => { // 获取窗口高度
  let windowHeight = 0
  if (document.compatMode === 'CSS1Compat') {
    windowHeight = document.documentElement.clientHeight
  } else {
    windowHeight = document.body.clientHeight
  }
  return windowHeight
}

// 浮点数乘法计算
export const accMul = (arg1, arg2) => {
  let m = 0
  let s1 = arg1.toString().trim()
  let s2 = arg2.toString().trim()
  try {
    m += s1.split('.')[1].length
  } catch (e) {}
  try {
    m += s2.split('.')[1].length
  } catch (e) {}
  let num1 = s1.replace('.', '')
  let num2 = s2.replace('.', '')
  return Number(num1) * Number(num2) / Math.pow(10, m)
}

// 是否为合法的手机号码
export const isMobilePhoneNum = (phone) => {
  return !isNaN(phone) && _reg.test(phone)
}
